//
//  TravelNotesViewController.m
//  旅游咨询
//
//  Created by 徐斌 on 15/4/13.
//  Copyright (c) 2015年 xubin. All rights reserved.
//

#import "TravelNotesViewController.h"
#import "MJRefresh.h"
@interface TravelNotesViewController ()

@end

@implementation TravelNotesViewController

- (void)dealloc
{
    [_TravelNotesTableView release];
    [_array release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.automaticallyAdjustsScrollViewInsets = YES;
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.tabBarController.tabBar.translucent = NO;
    self.TravelNotesTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.TravelNotesTableView];
    [_TravelNotesTableView release];
    self.array = [NSMutableArray array];
    self.TravelNotesTableView.delegate = self;
    self.TravelNotesTableView.dataSource = self;
    [self addHeader];
    [self addFooter];
    [_TravelNotesTableView release];
    

    
    // 取消tabview的线
    self.TravelNotesTableView.separatorStyle = NO;
   

    
    // Do any additional setup after loading the view.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSLog(@"---%ld",self.array.count);
    return self.array.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellFilr = @"cell";
    TravelNotesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellFilr];
    if (cell == nil) {
        
        cell = [[TravelNotesTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellFilr];
    }
    cell.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 3);
    cell.model = [self.array objectAtIndex:indexPath.row];
    cell.nameLabel.textColor = [UIColor whiteColor];
    cell.placeLabel.textColor = [UIColor whiteColor];
    cell.userlabel.textColor = [UIColor whiteColor];
 
    return cell;
  
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WidthZ * 0.75;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TravelNotesDetailViewController *detailVC = [[TravelNotesDetailViewController alloc]init];
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
    detailVC.idModel = [self.array objectAtIndex:indexPath.row];
//    detailVC.navigationController.tabBarController.tabBar.hidden = YES;
    [detailVC release];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}



- (void)addHeader
{
    // 在block块里面 使用self.可能会引起循环引用,声明成block变量__block
    //    __unsafe_unretained typeof(self) vc = self;(另一种方法)
    __block TravelNotesViewController *vc = self;
//    [vc getAFNData:0];
//     添加下拉刷新头部控件
    [self.TravelNotesTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        // 在block块里实现afn数据请求, 一定要写在这里
        vc.isUpLoading = NO;
        
        [vc getAFNData:0];
        
        
        NSLog(@"下拉刷新，开始网络请求第一页数据");
        
        
        
        
        
    }];
    
#pragma mark  --自动刷新(一进入程序就下拉刷新)
    [self.TravelNotesTableView headerBeginRefreshing];
}

#pragma mark -- 请求网络数据
- (void)getAFNData:(NSInteger)nextcursor
{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    // 参数参数隔开用&
    NSString *url_string = [NSString stringWithFormat:@"http://api.breadtrip.com/trips/hot/?start=%d&count=20&is_ipad=true",nextcursor];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        NSMutableDictionary *dic = responseObject;
        
        if (self.isUpLoading == NO) {
            
            [self.array removeAllObjects];
            
        }
        
        
        NSMutableArray *arr = [dic objectForKey:@"trips"];
        for (NSMutableDictionary *dic1 in arr) {
            TravelNotesModel *model = [[TravelNotesModel alloc]init];
            model.name = [dic1 objectForKey:@"name"];
            model.popular_place_str = [dic1 objectForKey:@"popular_place_str"];
            model.cover_image_default = [dic1 objectForKey:@"cover_image_w640"];
            model.DetailID = [dic1 objectForKey:@"id"];
            model.user = [dic1 objectForKey:@"user"];
//            NSLog(@"model %@",model.name);
            [self.array addObject:model];
//            NSLog(@"self %ld",self.array.count);
            [model release];
        }
        
        
        
        self.next_cursor = [[dic objectForKey:@"next_cursor"] integerValue];
        
        
//        NSLog(@"aaa= == = === = %@",responseObject);
        
        
        // 让下拉自动刷新停止
        [self.TravelNotesTableView headerEndRefreshing];

        // 刷新tableview
        [self.TravelNotesTableView reloadData];

        // 让上拉自动刷新停止
        [self.TravelNotesTableView footerEndRefreshing];
                
        
        // 如果页面是tableview 那么请求数据之后一定要刷新tableview
        // 如果页面是类似详情页面 label uiimageview 等其他控件 那么请求完数据 一定要给label uiimageview等控件重新赋值
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        // 弹出一个提示框提示用户当前网络不给力
        
        
        
        
        
        
        
    }];
    
    
    
}



#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block TravelNotesViewController *vc = self;
    
    vc.next_cursor +=20;
    // 添加上拉刷新尾部控件
    [self.TravelNotesTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        NSLog(@"上拉加载更多，开始网络请求更多数据");
        
        
        // 用AFN加载更多的数据
        [vc getAFNData:20];
        
        vc.isUpLoading = YES;
        
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
